﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SaleManagement
{
    public partial class loginForm : Form
    {
        public loginForm()
        {
            InitializeComponent();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            loginForm loginForm = new loginForm();
            QuanLi adminForm = new QuanLi();
            Staff nvForm = new Staff();
            RepositoryManager rpForm = new RepositoryManager();

            if (IsValidUser(txtUsername.Text, txtPassword.Text) == true)
            {
                loginForm.Hide();
                MessageBox.Show("Login successfully!!!");
                //adminForm.Show();
                nvForm.Show();
                //rpForm.Show();
            }
            else
            {
                MessageBox.Show("Invalid Username or Password!!!");
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        public bool IsValidUser(string username, string password)
        {
            SaleManagementDataContext sm = new SaleManagementDataContext();
            var q = from t in sm.Users 
                    join s in sm.Roles
                    on t.RoleId equals s.RoleId
                    where t.Username == txtUsername.Text
                    && t.Password == txtPassword.Text
                    //&& s.RoleName == comboBox1.SelectedText
                    select t;

            if (q.Any())
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public void FillCombobox()
        { 
            SaleManagementDataContext sm = new SaleManagementDataContext();

            //Lấy giá trị text đưa vào Combobox
            var value = (from p in sm.Roles
                         select new 
                         { 
                            roleId = p.RoleId,
                            roleName = p.RoleName
                         }
                        ).ToList();
            comboBox1.Items.Insert(0, "");
            this.comboBox1.DataSource = value;
            this.comboBox1.DisplayMember = "roleName";
            this.comboBox1.ValueMember = "roleId";
            //this.comboBox1.SelectedValue = SaleManagementDataContext.roleId.ToString();

        }
       
    }
}
